package com.omni.dao.impl;

import java.util.List;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.omni.dao.clause.WhereClause;
import com.omni.dao.clause.OrderClause;
import com.omni.dao.clause.PagingClause;
import com.omni.dao.entry.Group;
import com.omni.dao.GroupDAO;
import com.omni.dao.mapper.GroupRowMapper;

/**
 * This class is generated by codegen.
 * DO NOT MODIFY IT BY MANULLY!
 * @author OmniKennect.com
 */
public class GroupDAOImpl extends GroupDAO
{

	private final static Logger logger = LoggerFactory.getLogger (GroupDAOImpl.class);

	protected final static String SQLCount		= "SELECT COUNT(1) FROM group";
	protected final static String SQLDelById	= "DELETE FROM group WHERE tenantid=?";
	protected final static String SQLFindById	= "SELECT * FROM group WHERE tenantid=?";
	protected final static String SQLFindGeneral	= "SELECT * FROM group";
	protected final static String SQLUpdate		=
		"UPDATE group SET `gname`=?  ,`desc`=?  ,`createdate`=?  ,`updatedate`=?  WHERE tenantid=?";
	protected final static String SQLInsert		=
		"INSERT INTO group(`gname`  ,`desc`  ,`createdate`  ,`updatedate` )" +
				 "VALUES(?,?,?,?)";
	public GroupDAOImpl ()
	{

	}

	@Override
	public int count() {
		return this.jdbcTemplate.queryForObject(SQLCount, Integer.class);
	}

	@Override
	public int count(WhereClause expr) {
		return this.jdbcTemplate.queryForObject(SQLCount + " WHERE " + expr.toString(), Integer.class);
	}

	@Override
	public int delete(int id) {
		return this.jdbcTemplate.update(SQLDelById, new Object[] { id }, new int[] { Types.INTEGER });
	}

	@Override
	public boolean exists(int id) {
		return count(WhereClause.create().and("tenantid=?", Types.INTEGER, id)) > 0;
	}

	@Override
	public Group findOne(int id) {
		@SuppressWarnings("unchecked")
		final List<Group> result = jdbcTemplate.query(SQLFindById, new Object[]{ id }, new GroupRowMapper());
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public List<Group> findAll(WhereClause whereExpr) {
		return findAll(whereExpr, null, null);
	}

	@Override
	public List<Group> findAll(WhereClause whereExpr, OrderClause orderExpr) {
		return findAll(whereExpr, orderExpr, null);
	}

	@Override
	public List<Group> findAll(WhereClause whereExpr, OrderClause orderExpr, PagingClause pageExpr) {
		String sql = SQLFindGeneral;
		if (whereExpr != null)
			sql += " WHERE " + whereExpr.toString();

		if (orderExpr != null)
			sql += " ORDER BY " + orderExpr.toString();

		if (pageExpr != null)
			sql += " LIMIT " + pageExpr.toString();

		return jdbcTemplate.query(sql, new GroupRowMapper());
	}

	@Override
	public int save(Group entity) {
		if (entity.isNew()) {
			return insertEntity(entity);
		} else {
			return updateEntity(entity);
		}
	}

	private int updateEntity (final Group entity) {
		return this.jdbcTemplate.update(
			SQLUpdate,
			new Object[] {
				entity.getGname(),
				entity.getDesc(),
				entity.getCreatedate(),
				entity.getUpdatedate(),
				entity.getTenantid()
			},
			new int[]{
				Types.VARCHAR,
				Types.VARCHAR,
				Types.DATE,
				Types.DATE,
				Types.INTEGER
		});
	}

	private int insertEntity (final Group entity) {
		int ret = 0;
		KeyHolder keyHolder = new GeneratedKeyHolder();
		ret = this.jdbcTemplate.update(
				new PreparedStatementCreator() {

					public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

						PreparedStatement ps = (PreparedStatement) con.prepareStatement(
							SQLInsert, Statement.RETURN_GENERATED_KEYS);

						ps.setObject(1, entity.getGname(), Types.VARCHAR);
						ps.setObject(2, entity.getDesc(), Types.VARCHAR);
						ps.setObject(3, entity.getCreatedate(), Types.DATE);
						ps.setObject(4, entity.getUpdatedate(), Types.DATE);
						return ps;
					}
				}, keyHolder);

		entity.setTenantid( keyHolder.getKey().intValue());

		return ret;
	}

	/* START CUSTOMIZE CODE.*/

	/* END CUSTOMIZE CODE*/

}